﻿package toolbar {
	import flash.events.MouseEvent;
	import toolbar.icon.Selected;
	import util.EventParser;
	
	public class SelectButton extends IconButton {
		protected var btnGroup:SingleButtonGroup;
		protected var state:Boolean;
		
		public function SelectButton(intro:String, btnGroup:SingleButtonGroup = null, bgWidth:int = 22):void {
			super(intro, bgWidth);
			this.btnGroup = btnGroup;
			//默认为不选中状态
			state = false;
			//鼠标点击事件侦听
			addEventListener(MouseEvent.CLICK, onClickHandler);
		}
		
		protected function onClickHandler(evt:MouseEvent):void {
			if (!useSwitch) {
				return;
			}
			if(btnGroup != null) {
				btnGroup.cancelAll(this);
			}
			//切换状态
			if (state) {
				state = false;
				selected.visible = false;
				EventParser.dispatch(intro + 1);
			}
			else {
				state = true;
				selected.visible = true;
				EventParser.dispatch(intro);
			}
		}
		
		public function cancel():void {
			state = false;
			selected.visible = false;
		}
		public function active():void {
			state = true;
			selected.visible = true;
		}
	}
}